#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
创建系统音频文件
Create System Sound Files

生成基本的系统提示音文件
Generate basic system sound files
"""

import numpy as np
import wave
import os
from pathlib import Path

def create_beep(filename, frequency=1000, duration=0.5, sample_rate=44100):
    """
    创建蜂鸣音
    Create beep sound
    """
    t = np.linspace(0, duration, int(sample_rate * duration), False)
    # 生成正弦波
    wave_data = np.sin(frequency * 2 * np.pi * t)
    # 添加淡入淡出效果
    fade_frames = int(0.01 * sample_rate)  # 10ms fade
    wave_data[:fade_frames] *= np.linspace(0, 1, fade_frames)
    wave_data[-fade_frames:] *= np.linspace(1, 0, fade_frames)
    
    # 转换为16位整数
    wave_data = (wave_data * 32767).astype(np.int16)
    
    # 写入WAV文件
    with wave.open(filename, 'w') as wav_file:
        wav_file.setnchannels(1)  # 单声道
        wav_file.setsampwidth(2)  # 16位
        wav_file.setframerate(sample_rate)
        wav_file.writeframes(wave_data.tobytes())

def create_success_sound(filename, sample_rate=44100):
    """
    创建成功提示音（上升音调）
    Create success sound (ascending tone)
    """
    duration = 0.3
    t = np.linspace(0, duration, int(sample_rate * duration), False)
    # 频率从500Hz上升到1000Hz
    frequency = 500 + 500 * t / duration
    wave_data = np.sin(2 * np.pi * frequency * t)
    
    # 添加淡入淡出
    fade_frames = int(0.01 * sample_rate)
    wave_data[:fade_frames] *= np.linspace(0, 1, fade_frames)
    wave_data[-fade_frames:] *= np.linspace(1, 0, fade_frames)
    
    wave_data = (wave_data * 32767 * 0.5).astype(np.int16)
    
    with wave.open(filename, 'w') as wav_file:
        wav_file.setnchannels(1)
        wav_file.setsampwidth(2)
        wav_file.setframerate(sample_rate)
        wav_file.writeframes(wave_data.tobytes())

def create_error_sound(filename, sample_rate=44100):
    """
    创建错误提示音（下降音调）
    Create error sound (descending tone)
    """
    duration = 0.4
    t = np.linspace(0, duration, int(sample_rate * duration), False)
    # 频率从800Hz下降到300Hz
    frequency = 800 - 500 * t / duration
    wave_data = np.sin(2 * np.pi * frequency * t)
    
    # 添加淡入淡出
    fade_frames = int(0.01 * sample_rate)
    wave_data[:fade_frames] *= np.linspace(0, 1, fade_frames)
    wave_data[-fade_frames:] *= np.linspace(1, 0, fade_frames)
    
    wave_data = (wave_data * 32767 * 0.5).astype(np.int16)
    
    with wave.open(filename, 'w') as wav_file:
        wav_file.setnchannels(1)
        wav_file.setsampwidth(2)
        wav_file.setframerate(sample_rate)
        wav_file.writeframes(wave_data.tobytes())

def create_wake_sound(filename, sample_rate=44100):
    """
    创建唤醒提示音（双音调）
    Create wake sound (dual tone)
    """
    duration = 0.2
    t = np.linspace(0, duration, int(sample_rate * duration), False)
    # 混合两个频率
    wave_data = (np.sin(2 * np.pi * 800 * t) + np.sin(2 * np.pi * 1200 * t)) / 2
    
    # 添加淡入淡出
    fade_frames = int(0.01 * sample_rate)
    wave_data[:fade_frames] *= np.linspace(0, 1, fade_frames)
    wave_data[-fade_frames:] *= np.linspace(1, 0, fade_frames)
    
    wave_data = (wave_data * 32767 * 0.4).astype(np.int16)
    
    with wave.open(filename, 'w') as wav_file:
        wav_file.setnchannels(1)
        wav_file.setsampwidth(2)
        wav_file.setframerate(sample_rate)
        wav_file.writeframes(wave_data.tobytes())

def main():
    """
    主函数：创建所有系统音频文件
    Main function: Create all system sound files
    """
    # 获取音频目录路径
    current_dir = Path(__file__).parent
    audio_dir = current_dir.parent / "audio" / "system_sounds"
    
    # 确保目录存在
    audio_dir.mkdir(parents=True, exist_ok=True)
    
    print(f"Creating system sound files in: {audio_dir}")
    
    try:
        # 创建各种音频文件
        create_beep(str(audio_dir / "beep.wav"))
        print("✓ Created beep.wav")
        
        create_success_sound(str(audio_dir / "success.wav"))
        print("✓ Created success.wav")
        
        create_error_sound(str(audio_dir / "error.wav"))
        print("✓ Created error.wav")
        
        create_wake_sound(str(audio_dir / "wake_up.wav"))
        print("✓ Created wake_up.wav")
        
        create_beep(str(audio_dir / "start_record.wav"), frequency=1200, duration=0.2)
        print("✓ Created start_record.wav")
        
        create_beep(str(audio_dir / "stop_record.wav"), frequency=800, duration=0.2)
        print("✓ Created stop_record.wav")
        
        print("\nAll system sound files created successfully!")
        
    except Exception as e:
        print(f"Error creating sound files: {e}")
        return False
    
    return True

if __name__ == "__main__":
    main()